﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Reporting.WebForms;
using System.Data;
using System.Data.SqlClient;

namespace Dragon
{
    public partial class wfSalesByDis : System.Web.UI.Page
    {
        DataService ds = new DataService();

        protected void Page_Load(object sender, EventArgs e)
        {
            //List<string> roles = new List<string> { "All", "ASM", "SS" };
            //if (Session["Username"] == null || roles.Contains(Session["UserType"].ToString()) == false)
            //{
            //    Response.Redirect("~/wfLogin.aspx");
            //}

            if (!IsPostBack)
            {
                this.rpSalesByDis.Visible = false;
                this.ddlMonth.Text = DateTime.Now.ToString("MM");
                this.ddlYear.Text = DateTime.Now.ToString("yyyy");
                this.ddlProvince.SelectedIndex = 0;
                this.ddlDistributor.SelectedIndex = 0;
            }
        }

        protected void ddlProvince_DataBound(object sender, EventArgs e)
        {
            this.ddlProvince.Items.Insert(0, new ListItem("Tất cả", ""));
            this.ddlProvince.SelectedIndex = 0;
        }

        protected void ddlDistributor_DataBound(object sender, EventArgs e)
        {
            this.ddlDistributor.Items.Insert(0, new ListItem("Tất cả", ""));
            this.ddlDistributor.SelectedIndex = 0;
        }

        protected void btnReport_Click(object sender, EventArgs e)
        {
            string provinceCode = this.ddlProvince.SelectedValue.ToString();
            string provinceName = this.ddlProvince.SelectedItem.ToString();

            string distributorCode, distributorName;
            if (this.ddlDistributor.SelectedIndex != 0)
            {
                distributorCode = this.ddlDistributor.SelectedValue.ToString();
                distributorName = this.ddlDistributor.SelectedItem.ToString();
            }
            else
            {
                distributorCode = "";
                distributorName = "Tất cả";
            }    

            string sql = "exec sp_Report_Sales_By_Dis @Month, @Year, @ProvinceCode, @DistributorCode";
            SqlParameter pMonth = new SqlParameter("@Month", Convert.ToInt16(this.ddlMonth.Text));
            SqlParameter pYear = new SqlParameter("@Year", Convert.ToInt16(this.ddlYear.Text));
            SqlParameter pProvinceCode = new SqlParameter("@ProvinceCode", provinceCode);
            SqlParameter pDistributorCode = new SqlParameter("@DistributorCode", distributorCode);
            ds.SetData(sql, pMonth, pYear, pProvinceCode, pDistributorCode);

            this.rpSalesByDis.Visible = true;
            ReportParameter[] parameters = new ReportParameter[3];
            parameters[0] = new ReportParameter("Month", this.ddlMonth.Text);
            parameters[1] = new ReportParameter("Year", this.ddlYear.Text);
            parameters[2] = new ReportParameter("Province", provinceName);
            this.rpSalesByDis.LocalReport.SetParameters(parameters);

            DataTable dt = ds.GetData("exec sp_Display_Sales_By_Dis");
            ReportDataSource rds = new ReportDataSource("dsSalesByDis", dt);
            rpSalesByDis.LocalReport.DataSources.Clear();
            rpSalesByDis.LocalReport.DataSources.Add(rds);
            rpSalesByDis.LocalReport.Refresh();
        }
    }
}